<?php

function assign_job($line,$ln)
{
	$month = array('Jan' => 1, 'Feb' => 2, 'Mar' => 3, 'Apr' => 4, 'May' => 5, 'Jun' => 6, 'Jul' => 7, 'Aug' => 8, 'Sep' => 9, 'Oct' => 10, 'Nov' => 11, 'Dec' => 12);
	$year = date('Y');
	preg_match('/(?P<date1>^[A-Z]{1}[a-z]{2}\s[0-9]{2}\s[0-9]{2}:[0-9]{2}:[0-9]{2}) (?P<ip>[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}) (?P<id>[0-9]{1,9}): (?P<date2>([A-Z]{1}[a-z]{2}\s[0-9]{2}\s[0-9]{2}:[0-9]{2}:[0-9]{2}|.+)): %(?P<code1>.+)-(?P<code2>.+)-(?P<code3>.+?):(?P<msg>.+)/', $line, $matches);
	
	if(preg_match("/local Listening for Syslog messages/",$line))
	{
		return $ln." -- local Listening for Syslog messages";
	}
	else
	{
		$code = $matches['code1'].'-'.$matches['code2'].'-'.$matches['code3'];
	}
	
	if($code == 'LINK-3-UPDOWN')
	{
		if(preg_match("/changed state to up/",$matches['msg']))
		{
			$ip = new Ip;
			$state = new Error;
			$state->where('ip_id = '.$ip->get_by_wan($matches['ip'])->id.' and facility_id = 274 and severity_id = 4 and mnemonic_id = 18 and status = \'Pending\'')->order_by('created desc')->get();
			if($state->exists())
			{
				$state->close_date = date("Y-m-d H:i:s");
				$state->status = "Closed";
				$state->user_message = "Closed by System";
				$state->save();
				return $ln." -- Datetime: ".$state->errordate.'<br />IP Address: '.$matches['ip'].'<br />Error: '.$code.'<br />Status: Job Closed';
			}
			else
			{
				return $ln." -- ".$line;
			}
		}
		else
		{
			$ip = new Ip;
		exec('ping '.$ip->get_by_wan($matches['ip'])->loopback, $data);
			if(preg_match("/Reply from/",$data[2]))
			{
				$assignLevel = '2';
			}
			else
			{
				$assignLevel = '3';
			}
			$assign = new Assign;
			$assign->where("error_message like '%$code%'")->get();
			if($assign->exists())
			{
				$email['error'] = $code;
				$email['explanation'] = $assign->explanation;
				$email['recommended'] = $assign->recommended;				
			}	
		}
	}
	else
	{
		$assign_match = new Assign_match;
		$assign_match->where("error_message like '%$code%'")->get();
		if($assign_match->exists())
		{
			$assign = new Assign($assign_match->assign_id);
			preg_match('/%(?P<code1>.+)-(?P<code2>.+)-(?P<code3>.+?):(?P<msg>.+)/', $assign->error_message, $matches2);
			$ip = new Ip;
			$facility = new Facility;
			$severity = new Severity;
			$mnemonic = new Mnemonic;
			$state = new Error;
			$state->where('ip_id = '.$ip->get_by_wan($matches['ip'])->id.' and facility_id = '.$facility->get_by_code($matches2['code1'])->id.' and severity_id = '.$severity->get_by_code($matches2['code2'])->id.' and mnemonic_id = '.$mnemonic->get_by_name($matches2['code3'])->id.' and status = \'Pending\'')->order_by('created desc')->get();
			$state->close_date = date("Y-m-d H:i:s");
			$state->status = "Closed";
			$state->user_message = "Closed by System";
			$state->save();
			return $ln." -- Datetime: ".$state->errordate.'<br />IP Address: '.$matches['ip'].'<br />Error: '.$code.'<br />Status: Job Closed';
		}
		else
		{
			$ip = new Ip;
			$facility = new Facility;
			$severity = new Severity;
			$mnemonic = new Mnemonic;
			$mnemonic->get_by_name($matches['code3']);
			$checkError = new Error;
			$checkError->where('ip_id',$ip->get_by_wan($matches['ip'])->id);
			$checkError->where('facility_id',$facility->get_by_code($matches['code1'])->id);
			$checkError->where('severity_id',$severity->get_by_code($matches['code2'])->id);
			$checkError->where('mnemonic_id',$mnemonic->id);
			$checkError->where('status','Pending');
			$checkError->get();
			if(!$checkError->exists())
			{
				$assign = new Assign;
				$assign->where("error_message like '%$code%'")->get();
				if($assign->exists())
				{
					$email['error'] = $code;
					$email['explanation'] = $assign->explanation;
					$email['recommended'] = $assign->recommended;	
					$assignLevel = $assign->level_id;
				}
				else
				{
					if($matches['code2']<5)
					{
						$new = new New_message;
						$new->where('name',$code)->get();
						$new->name = $code;
						$new->save();
					}
					return $ln." -- ".$code.' not found in system';
				}
			}
			else
			{
				return $ln." -- ".$line;
			}
		}
	}
	
	if(isset($assignLevel))
	{
		$checkuser = new Error;
		$checkuser->sql("select users.*,ifnull(a.num,0) num
						from users left join (select user_id,count(user_id) num from errores where user_id in (select id from users where level_id = ".$assignLevel.") and status = 'Pending' group by user_id) a
						on a.user_id = users.id
						where users.level_id = ".$assignLevel."
						order by num
						")->get_page(1);
		$email['email'] = $checkuser->email;
		$ip = new Ip;
		$facility = new Facility;
		list($mon,$date,$time) = explode(' ', $matches['date1']);
		$severity = new Severity;
		$mnemonic = new Mnemonic;
		$mnemonic->get_by_name($matches['code3']);
		$mnemonic->name = $matches['code3'];
		$mnemonic->save();
		
		$array['user_id'] = $checkuser->id;
		$array['ip_id'] = $ip->get_by_wan($matches['ip'])->id;
		$array['facility_id'] = $facility->get_by_code($matches['code1'])->id;
		$array['error_date'] = $year.'-'.$month[$mon].'-'.$date.' '.$time;
		$array['severity_id'] = $severity->get_by_code($matches['code2'])->id;
		$array['mnemonic_id'] = $mnemonic->id;
		$array['msg'] = $matches['msg'];
						
		$error = new Error;
		$error->from_array($array);
		$error->save();
		$email['branch'] = $ip->branch->name;
		assign_mail($email);
		$log = $ln." -- Datetime: ".$array['error_date'].'<br />IP Address: '.$matches['ip'].'<br />Error: '.$code.'<br />Assing To: '.$checkuser->fullname;
		return $log;
	}
	else
	{
		return $ln." -- ".$line;
	}
	
}

function assign_mail($data)
{
	post("http://www.monofolio.com/home/abc_mail",$data);
}

function post($url,$data)
{
	$ch=curl_init();
	curl_setopt($ch,CURLOPT_URL,$url);
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));
	curl_exec($ch);
	curl_close($ch);
}
?>